﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;

namespace TFSPlus.Commands
{
    public class OpenSolutionNewDevenv : IAddinUICommand
    {
        #region IAddinUICommand Members

        public void UISetup(EnvDTE80.DTE2 applicationObject, EnvDTE.AddIn addInInstance, EnvDTE80.Commands2 commands, object[] contextGUIDS)
        {
            Command openSolutionInNewDevenvCommand = commands.AddNamedCommand2(addInInstance, "OpenSolutionNewDevenv", "Open Solution in New Environment", "Opens the selected solution in a new Visual Studio instance", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
            if (openSolutionInNewDevenvCommand != null)
            {
                // Add command to Explorer tool window shortcut menu
                CommandBar sourceControlExplorerCmdBar = ((CommandBars)applicationObject.CommandBars)["Source Control Explorer"];
                CommandBarButton openSolutionInNewDevenvShortcutCtrl = (CommandBarButton)openSolutionInNewDevenvCommand.AddControl(sourceControlExplorerCmdBar, 1);
                openSolutionInNewDevenvShortcutCtrl.FaceId = 23;
            }

        }

        public void Startup(EnvDTE80.DTE2 applicationObject, EnvDTE.AddIn addInInstance)
        {
            
        }

        public EnvDTE.vsCommandStatus QueryStatus()
        {
            if (Connect.VersionControlExt == null || Connect.VersionControlExt.Explorer == null)
                return vsCommandStatus.vsCommandStatusInvisible;

            bool isSolution = true;
            foreach (VersionControlExplorerItem vcei in Connect.VersionControlExt.Explorer.SelectedItems)
            {
                string lowered = vcei.SourceServerPath.ToLower();
                if (!lowered.EndsWith(".sln"))
                    isSolution = false;
            }

            if (isSolution)
                return vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported;
            else
                return vsCommandStatus.vsCommandStatusInvisible | vsCommandStatus.vsCommandStatusSupported;
        }

        public void Exec()
        {
            foreach (VersionControlExplorerItem vcei in Connect.VersionControlExt.Explorer.SelectedItems)
            {
                if (File.Exists(vcei.LocalPath))
                {
                    System.Diagnostics.Process.Start(vcei.LocalPath);
                }
            }

        }

        public void Disconnect(DTE2 applicationObject, AddIn addInInstance)
        {
            
        }

        #endregion
    }
}
